﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyGameServer.Model;
using NHibernate;
using NHibernate.Criterion;
namespace MyGameServer.Manager
{
    class UserManager : IUserManager
    {
        public void Add(User user)
        {
            //方法一
            //ISession session = NHiberateHelper.OPenSession();
            //session.Save(user);
            //session.Close();
            //---方法2
            //using (ISession session = NHiberateHelper.OPenSession())
            //{
            //    session.Save(user);
            //}

            //事务
            using (ISession session = NHiberateHelper.OPenSession())
            {
                using (ITransaction transaction= session.BeginTransaction())
                {
                    session.Save(user);
                    transaction.Commit();
                }
            }
        }

        /// <summary>
        /// 集合查询
        /// </summary>
        /// <returns></returns>
        public ICollection<User> GetAllUsers()
        {
            using (ISession session = NHiberateHelper.OPenSession())
            {
                //指定要查询的表（可以是用户表，也可以是装备表）
                ICriteria criteria = session.CreateCriteria(typeof(User));
                IList<User> users= criteria.List<User>();
                return users;
            }
        }

        public User GetById(int id)
        {
            using (ISession session = NHiberateHelper.OPenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
            User user=        session.Get<User>(id);
                    transaction.Commit();
                    return user;
                }
            }
        }

        public User GetByUsername(string username)
        {
            using (ISession session = NHiberateHelper.OPenSession())
            {
                //指定要查询的表（可以是用户表，也可以是装备表）
                ICriteria criteria=     session.CreateCriteria(typeof(User));
                //添加查询条件 Restrictions严格的意思  Username是字段名，不是mysql里面的
                criteria.Add(Restrictions.Eq("Username", username));
                //返回User对象
               User user=  criteria.UniqueResult<User>();
                return user;
            }
        }
        /// <summary>
        /// 保证Id存在
        /// </summary>
        /// <param name="user"></param>
        public void Remove(User user)
        {
            using (ISession session = NHiberateHelper.OPenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Delete(user);
                    transaction.Commit();
                }
            }

        }
        /// <summary>
        /// 保证Id存在
        /// </summary>
        /// <param name="user"></param>
        public void Update(User user)
        {

            using (ISession session = NHiberateHelper.OPenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Update(user);
                    transaction.Commit();
                }
            }
        }

        public bool VerifyUser(string username, string passworld)
        {
            using (ISession session = NHiberateHelper.OPenSession())
            {
                User user = session
                    .CreateCriteria(typeof(User))
                    .Add(Restrictions.Eq("Username", username))
                    .Add(Restrictions.Eq("Password", passworld))
                    .UniqueResult<User>();
                if (user == null) return false;
                return true;
            }
        }
    }


}
